package com.bishe.demo.modules.account.controller;

import com.bishe.demo.modules.account.entity.Resource;
import com.bishe.demo.modules.account.service.ResourceService;
import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * post
     * localhost/api/resource
     * {"permission":"aaa","roles":[{"roleId":"1"},{"roleId":"2"}]}
     */
    @PostMapping(value = "/resource", consumes = "application/json")
    ResultEntity<Resource> insertResource(@RequestBody Resource resource) {
        return resourceService.insertResource(resource);
    }

    /**
     * put
     * localhost/api/resource
     * {"resourceId":1,"permission":"bbb"}
     */
    @PutMapping(value = "/resource", consumes = "application/json")
    ResultEntity<Resource> updateResource(@RequestBody Resource resource) {
        return resourceService.updateResource(resource);
    }

    /**
     * delete
     * localhost/api/resource/1
     */
    @DeleteMapping("/resource/{resourceId}")
    ResultEntity<Resource> deleteResourceByResourceId(@PathVariable int resourceId) {
        return resourceService.deleteResourceByResourceId(resourceId);
    }

    /**
     * get
     * localhost/api/resource/1
     */
    @GetMapping("/resource/{resourceId}")
    Resource getResourceByResourceId(@PathVariable int resourceId) {
        return resourceService.getResourceByResourceId(resourceId);
    }

    /**
     * get
     * localhost/api/resources/v2/1
     */
    @GetMapping("/resources/v2/{roleId}")
    List<Resource> getResourcesByRoleId(@PathVariable int roleId) {
        return resourceService.getResourcesByRoleId(roleId);
    }

    /**
     * post
     * localhost/api/resources
     * {"keyword":"b"}
     */
    @PostMapping(value = "/resources", consumes = "application/json")
    PageInfo<Resource> getResourcesBySearchBean(@RequestBody SearchBean searchBean) {
        return resourceService.getResourcesBySearchBean(searchBean);
    }
}
